# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# Author     ：skyTree
# version    ：python 3.11
# Description：
"""
from fastapi import APIRouter, HTTPException, Depends
from fastapi.security import OAuth2PasswordRequestForm

from core.Auth import create_access_token
from models.base import User

auth_router = APIRouter()


@auth_router.post("/token")
async def test_oath2(data: OAuth2PasswordRequestForm = Depends()):
    username = data.username
    user = await User.get_or_none(name=username)
    if not user:
        raise HTTPException(401, "该用户不存在!")
    jwt_data = {
        'user_id': user.id,
        'user_name': user.name
    }
    jwt_token = create_access_token(data=jwt_data)

    return {"access_token": jwt_token, "token_type": "bearer"}
